package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.AddressBookDto;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    // 新增地址
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBookDto addressBookDto){
        log.info("新增地址,接收到的参数是{}",addressBookDto);
        addressBookService.addAddressBook(addressBookDto);
        return Result.success();
    }

    // 查询登录用户所有地址
    @GetMapping("/list")
    public Result<List<AddressBook>> selectAddressBookAll(){
        log.info("查询登录用户所有地址");
        return Result.success(addressBookService.list());
    }

    // 查询默认地址
    @GetMapping("/default")
    public Result<AddressBook> selectByIsDefault(){
        log.info("查询默认地址");
        return Result.success(addressBookService.selectByIsDefault());
    }

    // 修改地址
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址,接收到的参数是{}",addressBook);
        return Result.success(addressBookService.updateById(addressBook));
    }

    // 根据id查询地址
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        log.info("根据id查询地址,接收到的参数是{}",id);
        return Result.success(addressBookService.getById(id));
    }

    // 根据id删除地址
    @DeleteMapping
    public Result deleteById(Long id){
        log.info("根据id删除地址,接收到的参数是{}",id);
        return Result.success(addressBookService.removeById(id));
    }

    // 设置默认地址
    @PutMapping("/default")
    public Result updateIsDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址,接收到的参数是{}",addressBook);
        addressBookService.updateIsDefault(addressBook);
        return Result.success();
    }
}
